package me.zingon.web;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class MySessionListener implements HttpSessionListener {

	public void sessionCreated(HttpSessionEvent event) {
		 HttpSession session = event.getSession();
         ServletContext application = session.getServletContext();
         
         // 在application范围由一个HashSet集保存所有的session
         ConcurrentHashMap sessions = (ConcurrentHashMap) application.getAttribute("sessions"); 
         if (sessions == null) {
                sessions = new ConcurrentHashMap(); 
                application.setAttribute("sessions", sessions);
         }  
         session.setAttribute("style", ".pj");
         sessions.put(session.getId(), session); 
	}

	public void sessionDestroyed(HttpSessionEvent event) {
		 HttpSession session = event.getSession();
         ServletContext application = session.getServletContext();
         ConcurrentHashMap sessions = (ConcurrentHashMap) application.getAttribute("sessions");  
         ConcurrentHashMap logs = (ConcurrentHashMap) application.getAttribute("logs");

         sessions.remove(session.getId());
	}

}
